package cn.frh.product.service.impl;

import cn.frh.basic.service.impl.BaiduAiAuditServiceImpl;
import cn.frh.basic.service.impl.BaseServiceImpl;
import cn.frh.basic.util.AjaxResult;
import cn.frh.basic.util.LoginContext;
import cn.frh.org.domain.Employee;
import cn.frh.org.mapper.EmployeeMapper;
import cn.frh.product.domain.Product;
import cn.frh.product.domain.ProductDetail;
import cn.frh.product.domain.ProductOnlineAuditLog;
import cn.frh.product.mapper.ProductDetailMapper;
import cn.frh.product.mapper.ProductMapper;
import cn.frh.product.mapper.ProductOnlineAuditLogMapper;
import cn.frh.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private BaiduAiAuditServiceImpl baiduAiAuditService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Override
    public void offsale(List<Long> ids, HttpServletRequest request) {
        productMapper.offsale(new Date(),ids);
    }

    @Override
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        for (Long id :ids){
            Product product = productMapper.loadById(id);
            //审核文本
            Boolean aBoolean = baiduAiAuditService.textAudit(product.getName());
            //审核图片
            Boolean bBoolean = baiduAiAuditService.imageAudit(product.getResources());
            //公共信息
            //日志对象
            ProductOnlineAuditLog log = new ProductOnlineAuditLog();
            log.setProductId(id);
            //登录信息
            Long logininfoId = LoginContext.getLogininfo(request).getId();
            Employee employee = employeeMapper.lodaByLogininfoId(logininfoId);
            //设置操作人id
            log.setAuditId(employee.getId());
            //判断 审核成功或失败
            if (aBoolean&&bBoolean){
                //审核通过
                //上架修改时间和状态
                productMapper.onsale(new Date(),id);
                //设置日志信息
                log.setState(1);//1通过 0失败
                log.setNote("审核通过");
                productOnlineAuditLogMapper.save(log);
            }else {
                //审核失败：记录审核日志
                log.setState(0);//0表示审核失败
                log.setNote("审核失败，图片和文本非法!!!");//审核日志
                productOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("审核失败，图片和文本非法!!!");
            }
        }
        return new AjaxResult();
    }


    @Override
    public void save(Product product) {
        super.save(product);
        //自增id设置给详情
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    @Override
    public void update(Product product) {
        super.update(product);
        //修改宠物详情的
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.update(productDetail);
        }
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
        productDetailMapper.removeByProductId(id);
    }

    @Override
    public void patchRemove(List<Long> ids) {
        super.patchRemove(ids);
        //删除宠物详情
        productDetailMapper.patchRemoveByPetId(ids);
    }
}
